package com.ruoyi.sl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.SlRwork;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlAttendance;
import com.ruoyi.sl.service.ISlAttendanceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出勤天数Controller
 * 
 * @author admin
 * @date 2024-06-11
 */
@RestController
@RequestMapping("/sl/attendance")
public class SlAttendanceController extends BaseController
{
    @Autowired
    private ISlAttendanceService slAttendanceService;

    /**
     * 查询出勤天数列表
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlAttendance slAttendance)
    {
        startPage();
        List<SlAttendance> list = slAttendanceService.selectSlAttendanceList(slAttendance);
        return getDataTable(list);
    }

    /**
     * 导出出勤天数列表
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:list')")
    @Log(title = "出勤天数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlAttendance slAttendance)
    {
        List<SlAttendance> list = slAttendanceService.selectSlAttendanceList(slAttendance);
        ExcelUtil<SlAttendance> util = new ExcelUtil<SlAttendance>(SlAttendance.class);
        util.exportExcel(response, list, "出勤天数数据");
    }

    /**
     * 根据报工日期和报工人查询出勤列表
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:list')")
    @PostMapping("/all")
    public AjaxResult getAllAttendance(@RequestBody SlRwork slRwork)
    {
        return success(slAttendanceService.selectSlAttendanceByRwork(slRwork));
    }

    /**
     * 获取出勤天数详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:query')")
    @GetMapping(value = "/{attendanceId}")
    public AjaxResult getInfo(@PathVariable("attendanceId") Long attendanceId)
    {
        return success(slAttendanceService.selectSlAttendanceByAttendanceId(attendanceId));
    }

    /**
     * 新增出勤天数
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:add')")
    @Log(title = "出勤天数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlAttendance slAttendance)
    {
        slAttendance.setCreateBy(getUsername());
        return toAjax(slAttendanceService.insertSlAttendance(slAttendance));
    }

    /**
     * 修改出勤天数
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:edit')")
    @Log(title = "出勤天数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlAttendance slAttendance)
    {
        slAttendance.setUpdateBy(getUsername());
        return toAjax(slAttendanceService.updateSlAttendance(slAttendance));
    }

    /**
     * 修改出勤天数list
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:edit')")
    @Log(title = "出勤天数", businessType = BusinessType.UPDATE)
    @PutMapping("/upal")
    public AjaxResult updateAttendanceList(@RequestBody List<SlAttendance> slAttendanceList)
    {

        if (StringUtils.isNotNull(slAttendanceList)){
            for (SlAttendance slAttendance: slAttendanceList) {
                slAttendanceService.updateSlAttendance(slAttendance);
            }
        }
        return toAjax(slAttendanceList.size());
    }


    /**
     * 删除出勤天数
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:remove')")
    @Log(title = "出勤天数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{attendanceIds}")
    public AjaxResult remove(@PathVariable Long[] attendanceIds)
    {
        return toAjax(slAttendanceService.deleteSlAttendanceByAttendanceIds(attendanceIds));

    }
}
